package nl.saxion.customviews.beerology;

import nl.saxion.activity.beerology.R;
import nl.saxion.database.beerology.DatabaseQuery;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Toast;

public class Review extends LinearLayout
{
	
	private RadioGroup radioGroup;
	private EditText editTextLocation;
	private RatingBar ratingBar;
	private EditText editTextRemark;
	
	private Button buttonSave;
	private ButtonOnClickListener buttonOnClickListener;
	
	public Review(Context context)
	{
		super(context);
		
		initComponents();
	}
	
	
	public Review(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		
		initComponents();
	}
	
	/*
	 * Find the views by the given id.
	 */
	private void initComponents()
	{
		//Gets the modifiers.
		radioGroup = (RadioGroup) findViewById(R.id.review_radioGroup);
		editTextLocation = (EditText) findViewById(R.id.review_editTextLocation);
		ratingBar = (RatingBar) findViewById(R.id.review_rating);
		editTextRemark = (EditText) findViewById(R.id.review_editTextRemark);
		
		//Gets the save button and adds the OnClickListener.
		buttonSave = (Button) findViewById(R.id.review_buttonSave);
		buttonOnClickListener = new ButtonOnClickListener();
		buttonSave.setOnClickListener(buttonOnClickListener);
	}

	/*
	 * ButtonOnClickListener that handels the OnClick for the Buttons in this view.
	 */
	private class ButtonOnClickListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			if(v == buttonSave)
			{
				RadioButton radioButton = (RadioButton) radioGroup.getChildAt(radioGroup.getCheckedRadioButtonId());
				int type = 0;
				
				if(radioButton.getText().equals("Fles"))
				{
					type = 1;
				}
				else if(radioButton.getText().equals("Tap"))
				{
					type = 2;
				}
				else if(radioButton.getText().equals("Blik"))
				{
					type = 3;
				}
				
				String location = editTextLocation.getText().toString();
				float rating = ratingBar.getRating();
				String remark = editTextRemark.getText().toString();
				
				nl.saxion.model.beerology.Review review = new nl.saxion.model.beerology.Review(type, location, rating, remark);
				
				DatabaseQuery.newReview(review);
				
				Toast.makeText(getContext(), "Review toegevoegd.", Toast.LENGTH_SHORT).show();
			}
		}
		
	}
}
